singleApp.controller('postsortCtrl', ['$http', '$scope', '$sce', 'AppDataService', function ($http, $scope, $sce, AppDataService) {
    $scope.list = [];
    $scope.model_statuslist = AppDataService.StatusList;

    //Init
    $scope.init = function () {
        layer.load(2);
        var api_url = window.singleConfig.rootUrl + "api/admin/PostSortService/getAll";

        $http.get(api_url).then(function (result) {
            var data = result.data;
            $scope.list = data.data;
            layer.closeAll();
        }, function () { });
    };

    $scope.init();

    /*
     * add user
     */
    $scope.model_add = { id: 0, name: "", description: "", status: "1", type: "" };
    $scope.addNew = function (item) {
        if (item == null) {
            $scope.model_add = { id: 0, name: "", description: "", status: "1", type: "add" };
        }
        else {
            $scope.model_add = { id: item.id, name: item.name, description: item.description, status: item.status, type: "update" };
        }

        layer.open({
            type: 1,
            shade: false,
            title: item == null ? "Add New PostSort" : "Edit PostSort - " + item.name,
            content: $('#divAddNew'),
            area: ['600px', '350px'],
            cancel: function (index) {
                layer.closeAll();
            }
        });
    };

    $scope.confirmAddNew = function () {
        if ($scope.model_add.name == "" || $scope.model_add.description == "") {
            layer.msg("Please input all fields !");
            return;
        }

        api_url = window.singleConfig.rootUrl + 'api/admin/PostSortService/' + $scope.model_add.type;
        layer.load(2);
        $http({
            method: 'POST',
            url: api_url,
            data: $.param($scope.model_add),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
        .then(function (result) {
            var data = result.data;
            if (data.code == "success") {
                layer.closeAll();
                layer.msg("Add successful!");
                $scope.init();
            }
            else {
                layer.closeAll();
                layer.msg(data.message);
            }
        },function (data) {
            layer.closeAll();
            layer.msg("Add failed!");
        });
    };

    $scope.refresh = function (user) {
        layer.load(2);
        var api_url = window.singleConfig.rootUrl + "api/admin/UsersService/refresh?userid=" + user.id;
        $http.get(api_url).then(function (result) {
            var data = result.data;
            user.count_recipe = data.count;
            layer.closeAll();
        }, function () { });
    };

    /*
     * User Context Menu
     */
    $scope.menuOptionsUser = [
            ['Edit', function ($itemScope) {
                $scope.addNew($itemScope.item);
            }],
            null,
            ['Refresh', function ($itemScope) {
                
            }],
            null,
            ['Toggle Status', function ($itemScope) {
                layer.msg("Comming soon !");
            }]
    ];
}]);